package com.zhuge_new.Activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhuge_new.Adapter.RedDisponAdapter;
import com.zhuge_new.Adapter.RedMoneyimgAdapter;
import com.zhuge_new.Adapter.RedheadAdapter;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.bean.RedDisolayBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.wxapi.Constacts;

/**
 * Created by lenovo on 2018-09-13.
 */

public class RedDisplayActivity extends BaseActivity implements View.OnClickListener {

    private String redPacketTypeId;
    private ImageView lay_image;
    private TextView lay_name;
    private TextView lay_money;
    private TextView red_tale;
    private RecyclerView lay_grid;
    private RecyclerView lay_list;
    private Button lay_bleft;
    private Button lay_bright;
    private TextView red_num;
    private ImageView red_details;
    private IWXAPI wxApi;
    private RedDisolayBean redDisolayBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activityredisplay);
        Intent intent = getIntent();
        redPacketTypeId = intent.getStringExtra("redPacketTypeId");
        init_title();
        getData();
    }

    @Override
    public void init_title() {
        super.init_title();
        TextView tv_title = findViewById(R.id.tv_nav_title);
        tv_title.setText("红包详情");
        ImageView iv_back = findViewById(R.id.iv_nav_back);
        lay_image = findViewById(R.id.lay_image);
        lay_name = findViewById(R.id.lay_name);
        lay_money = findViewById(R.id.lay_money);
        red_tale = findViewById(R.id.red_tale);
        lay_grid = findViewById(R.id.lay_grid);
        lay_list = findViewById(R.id.lay_list);
        red_num = findViewById(R.id.red_num);
        lay_bleft = findViewById(R.id.lay_bleft);
        lay_bright = findViewById(R.id.lay_bright);
        red_details = findViewById(R.id.red_details);
        wxApi = WXAPIFactory.createWXAPI(baseContext, Constacts.WXAPPID);
        wxApi.registerApp(Constacts.WXAPPID);
        lay_grid.setLayoutManager(new GridLayoutManager(this, 3));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        lay_list.setLayoutManager(linearLayoutManager);
        iv_back.setOnClickListener(this);
        red_details.setOnClickListener(this);
        lay_bleft.setOnClickListener(this);
        lay_bright.setOnClickListener(this);
    }

    public void getData() {
        OkGo.<String>post(BaseHttp.selInfoByRedpacketId)
                .params("redpacketId", redPacketTypeId)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        redDisolayBean = (RedDisolayBean) new DataParse().getBean(response.body(), RedDisolayBean.class, 0);
                        lay_name.setText(redDisolayBean.getObject().getThreeNickName());
                        Glide.with(baseContext).load(redDisolayBean.getObject().getThreeHeadPhoto()).into(lay_image);
                        lay_money.setText(redDisolayBean.getObject().getMoney());
                        red_tale.setText(redDisolayBean.getObject().getStoryContent());
                        red_num.setText(redDisolayBean.getObject().getSum()+"/"+ redDisolayBean.getObject().getNum());
                        RedMoneyimgAdapter redMoneyimgAdapter = new RedMoneyimgAdapter(baseContext, redDisolayBean);
                        lay_grid.setAdapter(redMoneyimgAdapter);
                        RedheadAdapter redheadAdapter = new RedheadAdapter(baseContext, redDisolayBean);
                        lay_list.setAdapter(redheadAdapter);
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_nav_back: {
                ActivityStack.getScreenManager().popOneActivity(RedDisponAdapter.class);
                break;
            }
            case R.id.red_details:{
                Intent intent=new Intent(RedDisplayActivity.this,DetaileActivity.class);
                intent.putExtra("redpacketId",redPacketTypeId);
                startActivity(intent);
                break;
            }
            case R.id.lay_bleft:{
                sharewx(0);
                break;
            }
            case R.id.lay_bright:{
                Intent intent=new Intent(this, WebActivity.class);
                intent.putExtra("title","在线办卡");
                intent.putExtra("url","http://www.qichangkeji.vip/gongzhonghaoWeb/page/downCard.html?userId=5689");
                startActivity(intent);
                break;
            }
        }
    }

    //微信分享   0好友  1圈子
    private void sharewx(int flag) {
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = "http://web.zgzngj.com";
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = "诸葛智能管家";
        msg.description = "我发现一款超级好玩的APP,赶快下载抢红包";
        Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
        msg.setThumbImage(thumb);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        wxApi.sendReq(req);
    }
}
